package com.common.base.widget

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import android.view.WindowManager
import android.widget.Button
import android.widget.TextView
import com.common.base.R
import com.common.base.app.extras.otherwise
import com.common.base.app.extras.yes


/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：10/2/2023
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class CustomDialog(context: Context, buidler: Builder) :
    Dialog(context, R.style.custom_dialog_sv) {
    /**
     * Builder 对象
     */
    private val buidler: Builder
    var activity: Activity? = null

    /**
     * 执行方法
     */
    class Builder(context: Context) {
        /**
         * 上下文对象
         */
         val context: Context

        /**
         * dialogWidth
         */
         var dialogWidth = 0

        /**
         * 是否是平板
         */
         var isTablet = false

        /**
         * 可否关闭
         */
         var isConstraint = false

        /**
         * 标题栏
         */
         var title: String? = null

        /**
         * 消息第一栏
         */
         var message: String? = null

        /**
         * 消息第二栏
         */
         var message2: String? = null

        /**
         * 只有一个按钮的状态
         */
         var oneButtonText: String? = null

        /**
         * 左边按钮
         */
         var positiveButtonText: String? = null

        /**
         * 右边按钮
         */
         var negativeButtonText: String? = null

        /**
         * 只有一个按钮的状态
         */
         var oneButtonClickListener: DialogInterface.OnClickListener? = null

        /**
         * 左边按钮监听
         */
         var positiveButtonClickListener: DialogInterface.OnClickListener? = null

        /**
         * 右边按钮监听
         */
         var negativeButtonClickListener: DialogInterface.OnClickListener? = null

        /**
         * 设置context属性
         *
         * @param context context属性
         */
        init {
            this.context = context
        }

        /**
         * @param dialogWidth
         * @return this
         */
        fun setDialogWidth(dialogWidth: Int): Builder {
            this.dialogWidth = dialogWidth
            return this
        }

        /**
         * 设置约束强制
         *
         * @param falg
         * @return
         */
        fun setIsTablet(falg: Boolean): Builder {
            isTablet = falg
            return this
        }

        /**
         * 设置约束强制
         *
         * @param falg
         * @return
         */
        fun setConstraint(falg: Boolean): Builder {
            isConstraint = falg
            return this
        }

        /**
         * 设置message属性
         *
         * @param message1 message1属性
         * @return this
         */
        fun setMessage(message1: String?): Builder {
            message = message1
            return this
        }

        /**
         * 设置message属性
         *
         * @param message1 message1属性
         * @return this
         */
        fun setMessage(message1: Int): Builder {
            message = context.getText(message1).toString()
            return this
        }

        /**
         * 设置message2属性
         *
         * @param message2a message2属性
         * @return this
         */
        fun setMessage2(message2a: String?): Builder {
            message2 = message2a
            return this
        }

        /**
         * 设置message2a属性
         *
         * @param message3 message3属性
         * @return this
         */
        fun setMessage2(message3: Int): Builder {
            message2 = context.getText(message3).toString()
            return this
        }

        /**
         * 设置title属性
         *
         * @param title1 title1属性
         * @return this
         */
        fun setTitle(title1: Int): Builder {
            title = context.getText(title1).toString()
            return this
        }

        /**
         * 设置title2属性
         *
         * @param title2 title2属性
         * @return this
         */
        fun setTitle(title2: String?): Builder {
            title = title2
            return this
        }

        /**
         * 设置listener1属性
         *
         * @param positiveButtonText1 positiveButtonText1属性
         * @param listener1           listener1属性
         * @return this
         */
        fun setOneButton(
            positiveButtonText1: Int,
            listener1: DialogInterface.OnClickListener?
        ): Builder {
            oneButtonText = context.getText(positiveButtonText1).toString()
            oneButtonClickListener = listener1
            return this
        }

        /**
         * 设置positiveButtonText属性
         *
         * @param positiveButtonText2 positiveButtonText2属性
         * @param listener2           listener2属性
         * @return this
         */
        fun setOneButton(
            positiveButtonText2: String?,
            listener2: DialogInterface.OnClickListener?
        ): Builder {
            oneButtonText = positiveButtonText2
            oneButtonClickListener = listener2
            return this
        }

        /**
         * 设置listener1属性
         *
         * @param positiveButtonText1 positiveButtonText1属性
         * @param listener1           listener1属性
         * @return this
         */
        fun setPositiveButton(
            positiveButtonText1: Int,
            listener1: DialogInterface.OnClickListener?
        ): Builder {
            positiveButtonText = context.getText(positiveButtonText1).toString()
            positiveButtonClickListener = listener1
            return this
        }

        /**
         * 设置positiveButtonText属性
         *
         * @param positiveButtonText2 positiveButtonText2属性
         * @param listener2           listener2属性
         * @return this
         */
        fun setPositiveButton(
            positiveButtonText2: String?,
            listener2: DialogInterface.OnClickListener?
        ): Builder {
            positiveButtonText = positiveButtonText2
            positiveButtonClickListener = listener2
            return this
        }

        /**
         * 设置listener属性
         *
         * @param negativeButtonText1 negativeButtonText1属性
         * @param listener            listener属性
         * @return this
         */
        fun setNegativeButton(
            negativeButtonText1: Int,
            listener: DialogInterface.OnClickListener?
        ): Builder {
            negativeButtonText = context.getText(negativeButtonText1).toString()
            negativeButtonClickListener = listener
            return this
        }

        /**
         * 设置negativeButtonText属性
         *
         * @param negativeButtonText2 negativeButtonText2属性
         * @param listener            监听
         * @return this
         */
        fun setNegativeButton(
            negativeButtonText2: String?,
            listener: DialogInterface.OnClickListener?
        ): Builder {
            negativeButtonText = negativeButtonText2
            negativeButtonClickListener = listener
            return this
        }

        /**
         * 设置参数
         */
        fun create(): CustomDialog {
            return CustomDialog(context, this)
        }
    }

    /**
     * 构造方法
     *
     * @param context
     * @param buidler
     */
    init {
//        super(context, R.style.VerticalDialog);
        this.buidler = buidler
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.base_dialog_custom_white)
        if (buidler.isConstraint) {
            setCanceledOnTouchOutside(false)
        }

        // 获取title 参数
        (findViewById<View>(R.id.title) as TextView).text = buidler.title

        // 获取消息1参数
        (findViewById<View>(R.id.message) as TextView).text = buidler.message

        // 获取消息2参数
        (findViewById<View>(R.id.message2) as TextView).text = buidler.message2

        buidler.oneButtonText.isNullOrEmpty().yes {
            // 如果左按钮不为空
            if (buidler.positiveButtonText != null) {
                // 将文字键入按钮
                (findViewById<View>(R.id.positiveButton) as Button).text = buidler.positiveButtonText

                // 如果监听事件
                if (buidler.positiveButtonClickListener != null) {
                    // 调用监听接口
                    (findViewById<View>(R.id.positiveButton) as Button).setOnClickListener {
                        buidler.positiveButtonClickListener!!.onClick(
                            this@CustomDialog,
                            BUTTON_POSITIVE
                        )
                    }
                }
            } else {
                // 如果为空隐藏左键，,将背景样式左右圆角
                findViewById<View>(R.id.positiveButton).visibility = View.GONE
            }

            // 如果右按钮不为空
            if (buidler.negativeButtonText != null) {
                // 为右键设置文字
                (findViewById<View>(R.id.negativeButton) as Button).text = buidler.negativeButtonText

                // 如果右键有监听
                if (buidler.negativeButtonClickListener != null) {
                    // 为右键添加点击事件
                    (findViewById<View>(R.id.negativeButton) as Button).setOnClickListener {
                        buidler.negativeButtonClickListener!!.onClick(
                            this@CustomDialog,
                            BUTTON_NEGATIVE
                        )
                    }
                }
            } else {
                // 隐藏右键,将背景样式左右圆角
                findViewById<View>(R.id.negativeButton).visibility = View.GONE
            }
        }.otherwise {
            // 如果为空隐藏
            findViewById<View>(R.id.positiveButton).visibility = View.GONE
            // 将文字键入按钮
            (findViewById<View>(R.id.negativeButton) as Button).text = buidler.oneButtonText

            // 如果监听事件
            if (buidler.oneButtonClickListener != null) {
                // 调用监听接口
                (findViewById<View>(R.id.negativeButton) as Button).setOnClickListener {
                    buidler.oneButtonClickListener!!.onClick(
                        this@CustomDialog,
                        BUTTON_POSITIVE
                    )
                }
            }
        }

        // 如果消息1不为空
        if (!buidler.message.isNullOrEmpty()) {
            // 为消息设置文字
            (findViewById<View>(R.id.message) as TextView).text = buidler.message
        } else {
            // 如果为空隐藏布局
            findViewById<View>(R.id.message).visibility = View.GONE
        }

        // 如果消息2不为空
        if (!buidler.message2.isNullOrEmpty()) {
            // 为消息设置文字
            (findViewById<View>(R.id.message2) as TextView).text = buidler.message2
        } else {
            // 如果为空隐藏布局
            findViewById<View>(R.id.message2).visibility = View.GONE
        }

        // 如果有Title
        if (!buidler.title.isNullOrEmpty()) {
            // 获取title 参数
            (findViewById<View>(R.id.title) as TextView).text = buidler.title
        } else {
            findViewById<View>(R.id.title).visibility = View.GONE
        }
    }

    /**
     * 屏蔽返回键
     *
     * @param keyCode 对象
     * @param event   参数
     * @return KeyEvent
     * @see Dialog.onKeyDown
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (buidler.isConstraint) {

            // 屏蔽返回键
            when (keyCode) {
                KeyEvent.KEYCODE_BACK -> return true
            }
            return super.onKeyDown(keyCode, event!!)
        } else {
            dismiss()
        }
        return super.onKeyDown(keyCode, event!!)
    }

    override fun show() {
        val window = this.window
        window!!.decorView.setPadding(0, 0, 0, 0)
        val lp = window.attributes
        //设置宽
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        //设置高
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        window.attributes = lp
        //显示对话框
        window.setGravity(Gravity.CENTER)
        //设置dialog的动画效果
        window.setWindowAnimations(R.style.bottomShowStyle)
        super.show()
    }

    /**
     *
     */
    fun getTitleView(): TextView {
        return findViewById<View>(R.id.title) as TextView
    }

    /**
     *
     */
    fun getMsgView(): TextView {
        return findViewById<View>(R.id.message) as TextView
    }

    /**
     *
     */
    fun getMsg2View(): TextView {
        return findViewById<View>(R.id.message2) as TextView
    }
}
